package studychinese.activity;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.utils.AppUtils;
import studychinese.utils.ToastUtils;

public class SettingPwdActivity extends BaseAcitivity {


    @Bind(R.id.et_setpwd_psd)
    EditText etSetpwdPsd;
    @Bind(R.id.iv_setpwd_cliup)
    ImageView ivSetpwdCliup;
    @Bind(R.id.rl_setpwd_up)
    RelativeLayout rlSetpwdUp;
    @Bind(R.id.et_setpwdagin)
    EditText etSetpwdagin;
    @Bind(R.id.iv_setpwd_clidown)
    ImageView ivSetpwdClidown;
    @Bind(R.id.tv_login_point2)
    TextView tv_login_point2;
    @Bind(R.id.tv_login_point)
    TextView tv_login_point;
    @Bind(R.id.rl_setpsd_back)
    RelativeLayout rlSetpsdBack;
    @Bind(R.id.btn_setpwd_goregister)
    Button btnSetpwdGoregister;
    private Boolean showPassword = true;

    @Override
    protected int setContainer() {
        return R.layout.activity_setting_pwd;
    }

    @Override
    public void innitView() {
        ButterKnife.bind(this);
        etSetpwdPsd.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容

                } else {
                    // 此处为失去焦点时的处理内容
                    if (etSetpwdPsd.getText().length() < 6) {
                        tv_login_point.setVisibility(View.VISIBLE);
                    } else {
                        tv_login_point.setVisibility(View.GONE);
                    }
                }
            }
        });
    }


    @OnClick({R.id.iv_setpwd_cliup, R.id.iv_setpwd_clidown, R.id.btn_setpwd_goregister, R.id.iv_setpwd_go,R.id.left_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_setpwd_cliup:
                setIsShow(ivSetpwdCliup, etSetpwdPsd);
                break;
            case R.id.iv_setpwd_clidown:
                setIsShow(ivSetpwdClidown, etSetpwdagin);
                break;
            case R.id.left_img:
                this.finish();
                break;
            case R.id.btn_setpwd_goregister:
                startActivity(new Intent(this, LoginActivity.class));
                break;
            case R.id.iv_setpwd_go:

                String account = getIntent().getStringExtra("account");
                String setPsd = etSetpwdPsd.getText().toString().trim();
                String setPsdagin = etSetpwdagin.getText().toString().trim();

                if (setPsd.length() < 6) {
                    ToastUtils.showShort("密码最少是6位");
                } else if (!setPsd.equals(setPsdagin)) {
                    ToastUtils.showShort("两次密码不一致");
                } else {
                    setData(account, setPsd);
                }
                break;
        }
    }


    private void setData(String count, String pwd) {
        OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_REGISTERUSER))
                .params("user_name", count)
                .params("user_passwd", pwd)
                .tag(this)
                .execute(new DialogCallback<String>(this, true, "正在注册") {
                    @Override
                    public void onSuccess(Response<String> response) {

                        RegisterBean registerBean = JSON.parseObject(response.body(), RegisterBean.class);
                        if (registerBean.getState().equals(ConFigTag.state)) {
                            AppUtils.changeLogin(SettingPwdActivity.this, true);
                            String input = getIntent().getStringExtra("input");

                            if("tiaoguo".equals(input)){
                                RegisterBean.DataBean infor = UserBean.getInfor();
                                infor.setUser_id(registerBean.getData().getUser_id());
                                infor.setCreate_time(registerBean.getData().getCreate_time());
                                UserBean.putUserBean(infor);
                                Intent intent = new Intent(SettingPwdActivity.this, MySeleteBasicActivity.class);
                                startActivity(intent);
                            }else {
                                RegisterBean.DataBean dataBean = new RegisterBean.DataBean();
                                UserBean.setPerson(dataBean);
                                RegisterBean.DataBean infor = UserBean.getInfor();
                                infor.setUser_id(registerBean.getData().getUser_id());
                                infor.setCreate_time(registerBean.getData().getCreate_time());
                                UserBean.putUserBean(infor);
                                Intent intent = new Intent(SettingPwdActivity.this, WhatStudyActivity.class);
                                startActivity(intent);
                            }

                        } else {
                            ToastUtils.showShort(registerBean.getMessage());
                        }

                    }
                });


    }

    private void setIsShow(ImageView ivLoginPwCli, EditText etLoginPsd) {
        if (showPassword) {// 显示密码
            ivLoginPwCli.setImageDrawable(getResources().getDrawable(R.mipmap.login_showp_click));
            etLoginPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            etLoginPsd.setSelection(etLoginPsd.getText().toString().length());
            showPassword = !showPassword;
        } else {// 隐藏密码
            ivLoginPwCli.setImageDrawable(getResources().getDrawable(R.mipmap.login_unshowp_click));
            etLoginPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            etLoginPsd.setSelection(etLoginPsd.getText().toString().length());
            showPassword = !showPassword;
        }
    }




}
